package com.dony.web.controller.store;

import com.dony.common.annotation.Log;
import com.dony.common.core.controller.BaseController;
import com.dony.common.core.domain.AjaxResult;
import com.dony.common.core.page.TableDataInfo;
import com.dony.common.enums.BusinessType;
import com.dony.shop.domain.PublishTask;
import com.dony.shop.service.IPublishTaskService;
import com.dony.shop.service.IItemService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 商品发布Controller
 *
 * @author sundw
 * @date 2022-09-01
 */
@RestController
@RequestMapping("/store/publish")
public class PublishTaskController extends BaseController
{
    @Resource
    private IPublishTaskService publishTaskService;

    @Resource
    IItemService shopItemService;

    /**
     * 查询商品发布列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PublishTask publishTask)
    {
        startPage();
        List<PublishTask> list = publishTaskService.listByParam(publishTask);
        return getDataTable(list);
    }



    /**
     * 获取商品发布详细信息
     */
    @GetMapping(value = "/{publishTaskId}")
    public AjaxResult getInfo(@PathVariable("publishTaskId") Long publishTaskId)
    {
        return AjaxResult.success(publishTaskService.getById(publishTaskId));
    }

    /**
     * 同步发布状态
     */
    @GetMapping("/sync/{publishTaskIds}")
    public AjaxResult sync(@PathVariable("publishTaskIds") Long[] publishTaskIds)
    {
        publishTaskService.sycnStatus(publishTaskIds);
        return AjaxResult.success();
    }

    /**
     * 修改商品发布
     */
    @Log(title = "商品发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PublishTask publishTask)
    {
        return toAjax(publishTaskService.updateById(publishTask));
    }

    /**
     * 删除商品发布
     */
    @Log(title = "删除发布信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{publishTaskIds}")
    public AjaxResult remove(@PathVariable Long[] publishTaskIds)
    {
        return toAjax(publishTaskService.checkAndRemoveByIds(Arrays.asList(publishTaskIds)));
    }

    /**
     * 删除商品发布
     */
    @Log(title = "强制删除发布信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/force/{publishTaskIds}")
    public AjaxResult forceRemove(@PathVariable Long[] publishTaskIds)
    {
        return toAjax(publishTaskService.removeBatchByIds(Arrays.asList(publishTaskIds)));
    }
}
